<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import {reactive, ref} from "vue";


//定义数据模型
const registerDataRef = ref();
const registerData = reactive({
  username:'',
  password:'',
  rePassword:''
})

//二次校验密码的函数
const checkRePassword = (rule,value,callback) => {
  if(value === ''){
    callback(new Error('请再次确认密码'))
  } else if( value !== registerData.password){
    callback('二次确认密码不相同请重新输入')
  } else{
    callback()
  }
}

//定义表单校验规则
const registerRules = ref({
  username:[
    {required:true, message:'请输入用户名', trigger:'blur'},
    {min:1, max:16, message:'请输入长度1~16非空字符', trigger:'blur'}
  ],
  password:[
    {required:true, massage:'请输入密码', trigger:'blur'},
    {min:6, max:16, message:'请输入长度6~16非空字符', trigger:'blur'}
  ],
  rePassword:[{validator:checkRePassword, trigger:'blur'}] //校验二次输入密码是否相同
})


//调用后台接口完成注册
import api from '@/api/index.js'
const register_request = async () => {
  //registerData是一个响应式对象，调用时现需要加上.value
  let rq_data = JSON.parse(JSON.stringify(registerData))  // Proxy(Object)通过json转成字典
  console.log(rq_data)
  let result = await api.register(rq_data);
  console.log(result)
  if(result.code === 0) {
    //成功
    alert(result.msg ? result.massage:'注册成功');
  }else{
    //失败
    alert('注册失败')
  }
}

</script>

<template>
  <div class="back_ground">
    <el-row class="register-page">
      <el-col :span="5" :offset="18" class="form">
        <!-- 注册表单 -->
        <el-form ref="form" size="large" autocomplete="off" :model="registerData" :rules="registerRules" :ref="registerDataRef">
          <el-form-item>
            <h1>注册</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
          </el-form-item>
          <el-form-item prop="rePassword">
            <el-input :prefix-icon="Lock" type="password" placeholder="请输入确认密码" v-model="registerData.rePassword"></el-input>
          </el-form-item>
          <!-- 注册按钮 -->
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="register_request">
              注册
            </el-button>
          </el-form-item>
          <el-form-item>
            <router-link to="/login">登录</router-link>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
/* 样式 */
.back_ground {
  width: 100%;
  height: 100%;
  background: url('@/assets/login_bg.png') no-repeat center center;
  background-size: 100% 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.register-page {
  width: 100%;
  height: 100%;
}
.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  user-select: none;
}
.button {
  width: 100%;
}

.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

</style>
